import Vue from 'vue'
import moment from 'moment'
import 'moment/locale/zh-cn'
moment.locale('zh-cn')


Vue.filter('NumberFormat', function (value) {
  if (!value) {
    return '0'
  }
  const intPartFormat = value.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,') // 将整数部分逢三一断
  return intPartFormat
})

Vue.filter('dayjs', function (dataStr, pattern = 'YYYY-MM-DD HH:mm:ss') {
  return moment(dataStr).format(pattern)
})

Vue.filter('moment', function (dataStr, pattern = 'YYYY-MM-DD HH:mm:ss') {
  return moment(dataStr).format(pattern)
})

// 流程任务耗时过滤器
Vue.filter('timeConsuming', function (value) {
  // return value
  let time = value ? value : 0
  let ss = 1000
  let mi = ss * 60
  let hh = mi * 60
  let dd = hh * 24

  let day = Math.floor(time / dd)
  let hour = day >= 1 ?  (time - day * dd) / hh : Math.floor(time / hh)
  let minute = hour >= 1 ?  (time - day * dd - hour * hh) / mi : Math.floor(time / mi)
  let second = minute >= 1 ?  ((time - day * dd - hour * hh - minute * mi)) * 1.0 / ss : Math.round(time / ss)

  if (day >= 1) {
    return Math.floor(day) + '天' + Math.floor(hour) + '小时'
  } else if (hour >= 1) {
    return Math.floor(hour) + '小时' + Math.floor(minute) + '分钟'
  } else if (minute >= 1) {
    
    return Math.floor(minute) + '分钟'
  } else if (second >= 1) {
    return '1分钟'
  } else {
    return second + '秒'
  }
})

// 时间格式化过滤器
Vue.filter('getTimeFormat', function (time, format) {
  if(!time) {
    return ''
  }
  return moment(time).format(format)
})
